/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
package axescon.xacml.model.ctx;

import axescon.xacml.api.XacmlOptions;
import axescon.xacml.model.explain.Explanation;

/**
 * @author argyn
 *         Date: Oct 17, 2005
 *         Responsibilities:
 *         this decorator is used to add utility methods to normal request. particularly, to add
 *         decision explanation feature and session cache
 */
public class XacmlCtx implements Request {

    /**
     * request , whcih is being decorated by this class
     */
    Request req;

    /**
     * construct decorator from original request
     * @param req original request
     */
    public XacmlCtx(Request req) {
        this(req, null);
    }

    /**
     * construct decorator from original request using options
     * @param req original request
     * @param options
     */
    public XacmlCtx(Request req, XacmlOptions options) {
        this.req = req;
        this.options = (options == null ? new XacmlOptions() : options);
        if (getOptions().EXPLAIN) {
            explanation = new Explanation();
        }
    }

    public void setSubjectArray(Subject[] sl) {
        req.setSubjectArray(sl);
    }

    public void setResourceArray(Resource[] rl) {
        req.setResourceArray(rl);
    }

    public void setAction(RequestPart action) {
        req.setAction(action);
    }

    public void setEnvironment(RequestPart environment) {
        req.setEnvironment(environment);
    }

    public Subject[] getSubjectArray() {
        return req.getSubjectArray();
    }

    public Resource[] getResourceArray() {
        return req.getResourceArray();
    }

    public RequestPart getAction() {
        return req.getAction();
    }

    public RequestPart getEnvironment() {
        return req.getEnvironment();
    }

    public Object getLLObject() {
        return req.getLLObject();
    }

    /**
     * get original request
     * @return  original request
     */
    public Request getReq() {
        return req;
    }

    /**
     * sets original request
     * @param req original request
     */
    public void setReq(XacmlCtx req) {
        this.req = req;
    }


    public Explanation getExplanation() {
        return explanation;
    }

    public void setExplanation(Explanation explanation) {
        this.explanation = explanation;
    }

    /**
     * this object contains explanation of the result's decision
     */
    public Explanation explanation;

    public void addStep(String sentence) {
        explanation.addChild(sentence);
    }

    public void addSibling(String sentence) {
        explanation.addSibling(sentence);
    }

    public void addSentence(String sentence) {
        explanation.addSentence(sentence);
    }

    public void toParentStep() {
        explanation.toParent();
    }

    /**
     * options
     */
    XacmlOptions options;

    /**
     * get configuration options
     * @return configuration options
     */
    public XacmlOptions getOptions() {
        return options;
    }

    /**
     * sets configuration options
     * @param options configuration options             
     */
    public void setOptions(XacmlOptions options) {
        this.options = options;
    }

    /**
     * the index of the current request which is being evaluated
     */
    private int currentResourceIdx;

    public int getCurrentResourceIdx() {
        return currentResourceIdx;
    }

    public void setCurrentResourceIdx(int currentResourceIdx) {
        this.currentResourceIdx = currentResourceIdx;
    }


    /**
     * current request which is being evaluated
     */
    public Resource getCurrentResource() {
        return req.getResourceArray()[currentResourceIdx];
    }


}
